package com.ctsi.offlinesupport.service;


import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.offlinesupport.dto.CustomtjDTO;
import com.ctsi.security.ISecurity;
import com.ctsi.supportflow.app.entity.CustomEntity;
import com.ctsi.supportflow.app.entity.ProjectEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Service
public class CustomEntityService {
	private static final Logger logger=LoggerFactory.getLogger(CustomEntityService.class);
	@Autowired
	private MybatisGenericDao dao;

	@Autowired
	private ISecurity iSecurity;

	@Autowired
	private ProjectEntityService projectEntityService;

	/**
	 * 首页驾驶舱客户统计
	 */
	public Map<String,Object> selectLeaderCockpitCustom(){
		List<CustomtjDTO> o = (List<CustomtjDTO>) dao.queryForList(CustomEntity.class.getName() + ".selectLeaderCockpitCustom");
		Map<String,Object> map=new HashMap<>();
		for (CustomtjDTO customtjDTO :o){
			map.put(customtjDTO.getKey1(), customtjDTO.getNum());
		}
		return map;
	}
	/**
	 * 添加
	 */
	public void insertCustomEntity(CustomEntity customEntity){
		if(UtilValidate.isEmpty(customEntity.getId()))
			customEntity.setId(UUIDGenerator.getId());
		logger.debug("insert {}",customEntity);
		dao.insert(customEntity);
	}
	/**
	 * 动态更新
	 */
	public int updateCustomEntityByPKSelective(CustomEntity customEntity){
		logger.debug("updateByPrimaryKeySelective {}",customEntity);
		return dao.update(CustomEntity.class.getName()+".updateByPrimaryKeySelective", customEntity);
	}
	/**
	 * 更新
	 */
	public int updateCustomEntity(CustomEntity customEntity){
		logger.debug("update {}",customEntity);
		return dao.update(customEntity);
	}


	public CustomEntity editCustomEntityById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("editById {}",id);
		return (CustomEntity)dao.queryForObject(CustomEntity.class.getName()+".edit", id);
	}
	/**
	 * 根据ID查找 CustomEntity对象
	 */
	public CustomEntity findCustomEntityById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("findById {}",id);
		return dao.get(CustomEntity.class, id);
	}
	/**
	 * 删除CustomEntity对象
	 */
	public int removeCustomEntity(String id){
		if(UtilValidate.isNotEmpty(id))
			return dao.delete(CustomEntity.class.getName()+".delete", id);
			//return dao.remove(id);
		return 0;
	}

	/**
	 * 逻辑删除客户，删除客户得同时删除客户下得项目
	 * @param customId
	 * @return
	 */
	public int removeCustom(String customId){
		if (UtilValidate.isNotEmpty(customId)) {
			int i = 0;
			// 先逻辑删除客户
			CustomEntity custom = new CustomEntity();
			custom.setId(customId);
			custom.setIsDel(1);
			i = this.updateCustomEntityByPKSelective(custom);
			// 查询客户下得项目
			List<ProjectEntity> result = projectEntityService.findProjectByCoustomId(customId);
			result.stream().forEach(pro -> {
				ProjectEntity project = new ProjectEntity();
				project.setIsDel(1);
				project.setId(pro.getId());
				projectEntityService.updateProjectEntityByPKSelective(project);
			});
			return i;
		}
		return 0;
	}

//	/**
//	 * 是否可以删除该客户
//	 * @param customId
//	 * @return
//	 */
//	public boolean isCanRemoveCustom(String customId){
//		if (UtilValidate.isEmpty(customId)) {
//			logger.error("校验是否可以删除该客户时，customId 不能为空!");
//			throw new RuntimeException("客户ID不能为空!");
//		}
//		// 查询该客户下是否有支撑单
//		Page<SupportInfoDTO> result = supportInfoService.findSupportInfoByCustomId(customId,0,15);
//
//		return result.getTotalCount() == 0;
//	}



	/**
	 * 详细页查询
	 */
	public CustomEntity findByDetail(String id){
		return (CustomEntity)dao.queryForObject(CustomEntity.class.getName()+".findByDetail",id);
	}
	/**
	 * 多条件查询结果
	 */
	public Page<CustomEntity> findByMulitCondition(Map<String,Object> param ,String userLoginId,String userId, int start , int pageSize){
		logger.debug("findByMulitCondition {}",param);
		if(pageSize<0||pageSize>2000)
			pageSize=Page.DEFAULT_PAGE_SIZE;
		if(start!=-1) {
			if (!iSecurity.isControl(userLoginId,"organChain","SELECT")) {
				return dao.queryPage(CustomEntity.class.getName()+".selectMulitCondition", param, start, pageSize);
			}else {
				Set<String> dataObjectIds = iSecurity.findDataObjectIds(userLoginId,"organChain","SELECT");
				if (UtilValidate.isNotEmpty(dataObjectIds)) {
					param.put("dataObjectIds",dataObjectIds);
				} else {
					param.put("createBy",userId);
				}
				return dao.queryPage(CustomEntity.class.getName()+".selectMulitCondition", param, start, pageSize);
			}
		}
		else{
			throw new RuntimeException("参数不正确 !!");
		}
	}
	/**
	 * 多条件查询结果不分页
	 */
	public List<CustomEntity> find(Map<String,Object> map){
		logger.debug("find {}",map);
		return (List<CustomEntity>)dao.queryForList(CustomEntity.class.getName()+".selectByMap",map);
	}

}
